package com.atguigu.flightservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.flightservice.entity.Flight;
import com.atguigu.flightservice.entity.Seat;
import com.atguigu.flightservice.entity.flight.FlightInfo;
import com.atguigu.flightservice.entity.vo.FlightQuery;
import com.atguigu.flightservice.service.FlightService;
import com.atguigu.flightservice.service.SeatService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lwh
 * @since 2021-10-28
 */


@RestController//接口都是返回这种，@Controller  @ResponseBody
@Api(description="航班管理")
@CrossOrigin  //解决跨域
@RequestMapping("/flightservice/flight")
public class FlightController {

    //    注入service
    @Autowired
    private FlightService flightService;

    @Autowired
    private SeatService seatService;


    //根据讲师id进行查询
    @ApiOperation(value = "根据ID查询航班信息")
    @GetMapping("getFlightInfo/{id}")
    public R getFlightInfo(@PathVariable String id) {
        Flight flight = flightService.getById(id);
        String flightId = flight.getId();
        QueryWrapper<Seat> seatQueryWrapper = new QueryWrapper<>();
        seatQueryWrapper.eq("flight_id",flightId);
        List<Seat> seats = seatService.list(seatQueryWrapper);
        FlightInfo flightInfo = new FlightInfo();
        //flight值复制到对应flightInfo对象里面
        BeanUtils.copyProperties(flight,flightInfo);
        flightInfo.setChildren(seats);

        return R.ok().data("flight",flightInfo);
    }


    //1 查询所有的的数据
    //    访问地址  http://localhost:8001/eduservice/teacher/findAll
    @ApiOperation(value = "所有航班信息列表")
    @GetMapping("findAll")
    public R findAllFlight(){

        List<Flight> list = flightService.list(null);
        return R.ok().data("items",list);
        /**
         *       使用map集合，key value
         *         public R data(String key, Object value){
         *             this.data.put(key, value);
         *             return this;
         *         }
         */
    }


    @ApiOperation("所有航班信息带位置等级")
    @GetMapping("getAllFlight")
    public R getAllFlight() {
        //list集合泛型是一级分类
        List<Flight> flightList = flightService.list(null);
        List<Seat> seatList = seatService.list(null);
        //创建list集合，用于存储最终封装数据
        List<FlightInfo> flightInfoList = new ArrayList<>();

        for(Flight flight:flightList){
            String flightId = flight.getId();
            QueryWrapper<Seat> seatQueryWrapper = new QueryWrapper<>();
            seatQueryWrapper.eq("flight_id",flightId);
            List<Seat> seats = seatService.list(seatQueryWrapper);
            FlightInfo flightInfo = new FlightInfo();
            //flight值复制到对应flightInfo对象里面
            BeanUtils.copyProperties(flight,flightInfo);
            flightInfo.setChildren(seats);
            flightInfoList.add(flightInfo);
        }
        return R.ok().data("list",flightInfoList);
    }

    @ApiOperation("条件查询单程所有航班信息带位置等级列表")
    @PostMapping("getAllFlightList")
    public R getAllFlightList( @RequestBody(required = false) FlightQuery flightQuery)  throws ParseException{
        //list集合泛型是一级分类
        //构建条件
        QueryWrapper<Flight> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String flightNumber = flightQuery.getFlightNumber();
        String airline = flightQuery.getAirline();
        String takeoffAirport = flightQuery.getTakeoffCity();
        String arrivalAirport = flightQuery.getArrivalCity();
        String takeoffTime = flightQuery.getTakeoffTime();

        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(flightNumber)) {
            //构建条件,模糊查询
            wrapper.like("flight_number",flightNumber);
        }
        //构建条件,模糊查询
        if(!StringUtils.isEmpty(airline)) {
            wrapper.like("airline",airline);
        }
        //起飞地
        if(!StringUtils.isEmpty(takeoffAirport)) {
            wrapper.like("takeoff_city",takeoffAirport);
        }
        //到达地
        if(!StringUtils.isEmpty(arrivalAirport)) {
            wrapper.like("arrival_city",arrivalAirport);
        }
        // 日期大于等于
        if(!StringUtils.isEmpty(takeoffTime)) {
            wrapper.ge("takeoff_time",takeoffTime);

            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //定义日期格式化的格式
            Date classDate = format.parse(takeoffTime);//把字符串转化成指定格式的日期
            Calendar calendar = Calendar.getInstance(); //使用Calendar日历类对日期进行加减
            calendar.setTime(classDate);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            classDate = calendar.getTime();//获取加减以后的Date类型日期
            String day2 = format.format(classDate);//转化日历格式为上面格式字符串
            wrapper.le("takeoff_time",day2);
        }

        //根据日期进行排序
        wrapper.orderByDesc("takeoff_time");


        List<Flight> flightList = flightService.list(wrapper);
        List<Seat> seatList = seatService.list(null);
        //创建list集合，用于存储最终封装数据
        List<FlightInfo> flightInfoList = new ArrayList<>();

        for(Flight flight:flightList){
            String flightId = flight.getId();
            QueryWrapper<Seat> seatQueryWrapper = new QueryWrapper<>();
            seatQueryWrapper.eq("flight_id",flightId);
            List<Seat> seats = seatService.list(seatQueryWrapper);
            FlightInfo flightInfo = new FlightInfo();
            //flight值复制到对应flightInfo对象里面
            BeanUtils.copyProperties(flight,flightInfo);
            flightInfo.setChildren(seats);
            flightInfoList.add(flightInfo);
        }
        return R.ok().data("list",flightInfoList);
    }


    @ApiOperation("条件查询往返所有航班信息带位置等级列表")
    @PostMapping("getReturnFlightList")
    public R getReturnFlightList(@RequestBody(required = false) FlightQuery flightQuery) throws ParseException{
//        1、查询去的机票信息
        //list集合泛型是一级分类
        //构建条件
        QueryWrapper<Flight> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String flightNumber = flightQuery.getFlightNumber();
        String airline = flightQuery.getAirline();
        String takeoffAirport = flightQuery.getTakeoffCity();
        String arrivalAirport = flightQuery.getArrivalCity();
        String takeoffTime = flightQuery.getTakeoffTime();


        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(flightNumber)) {
            //构建条件,模糊查询
            wrapper.like("flight_number",flightNumber);
        }
        //构建条件,模糊查询
        if(!StringUtils.isEmpty(airline)) {
            wrapper.like("airline",airline);
        }
        //起飞地
        if(!StringUtils.isEmpty(takeoffAirport)) {
            wrapper.like("takeoff_city",takeoffAirport);
        }
        //目的地
        if(!StringUtils.isEmpty(arrivalAirport)) {
            wrapper.like("arrival_city",arrivalAirport);
        }
        // 日期大于等于
        if(!StringUtils.isEmpty(takeoffTime)) {
            wrapper.ge("takeoff_time",takeoffTime);

            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //定义日期格式化的格式
            Date classDate = format.parse(takeoffTime);//把字符串转化成指定格式的日期
            Calendar calendar = Calendar.getInstance(); //使用Calendar日历类对日期进行加减
            calendar.setTime(classDate);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            classDate = calendar.getTime();//获取加减以后的Date类型日期
            String day2 = format.format(classDate);//转化日历格式为上面格式字符串
            wrapper.le("takeoff_time",day2);
        }

        //根据日期进行排序
        wrapper.orderByDesc("takeoff_time");


        List<Flight> flightList = flightService.list(wrapper);
        List<Seat> seatList = seatService.list(null);
        //创建list集合，用于存储最终封装数据
        List<FlightInfo> flightInfoList = new ArrayList<>();

        for(Flight flight:flightList){
            String flightId = flight.getId();
            QueryWrapper<Seat> seatQueryWrapper = new QueryWrapper<>();
            seatQueryWrapper.eq("flight_id",flightId);
            List<Seat> seats = seatService.list(seatQueryWrapper);
            FlightInfo flightInfo = new FlightInfo();
            //flight值复制到对应flightInfo对象里面
            BeanUtils.copyProperties(flight,flightInfo);
            flightInfo.setChildren(seats);
            flightInfoList.add(flightInfo);
        }

//        2、查询返回的机票信息
        //构建条件
        QueryWrapper<Flight> queryWrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String returnFlightNumber = flightQuery.getFlightNumber();
        String returnAirline = flightQuery.getAirline();
        String returnTakeoffAirport = flightQuery.getTakeoffCity();
        String returnArrivalAirport = flightQuery.getArrivalCity();
        String returnTime = flightQuery.getReturnTime();

        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(returnFlightNumber)) {
            //构建条件,模糊查询
            queryWrapper.like("flight_number",returnFlightNumber);
        }
        //构建条件,模糊查询航空公司
        if(!StringUtils.isEmpty(returnAirline)) {
            queryWrapper.like("airline",returnAirline);
        }
        //起飞机场，这里起飞的返回就是到达的
        if(!StringUtils.isEmpty(returnArrivalAirport)) {
            queryWrapper.like("takeoff_city",returnArrivalAirport);
        }
        //到达机场，这里到达的就是起飞的
        if(!StringUtils.isEmpty(returnTakeoffAirport)) {
            queryWrapper.like("arrival_city",returnTakeoffAirport);
        }
        // 日期大于等于
        if(!StringUtils.isEmpty(returnTime)) {
            queryWrapper.ge("takeoff_time",returnTime);

            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //定义日期格式化的格式
            Date classDate = format.parse(returnTime);//把字符串转化成指定格式的日期
            Calendar calendar = Calendar.getInstance(); //使用Calendar日历类对日期进行加减
            calendar.setTime(classDate);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            classDate = calendar.getTime();//获取加减以后的Date类型日期
            String day2 = format.format(classDate);//转化日历格式为上面格式字符串
            queryWrapper.le("takeoff_time",day2);
        }

        //根据日期进行排序
        queryWrapper.orderByDesc("takeoff_time");


        List<Flight> returnFlightList = flightService.list(queryWrapper);
        List<Seat> returnSeatList = seatService.list(null);
        //创建list集合，用于存储最终封装数据
        List<FlightInfo> returnFlightInfoList = new ArrayList<>();

        for(Flight flight:returnFlightList){
            String flightId = flight.getId();
            QueryWrapper<Seat> seatQueryWrapper = new QueryWrapper<>();
            seatQueryWrapper.eq("flight_id",flightId);
            List<Seat> seats = seatService.list(seatQueryWrapper);
            FlightInfo flightInfo = new FlightInfo();
            //flight值复制到对应flightInfo对象里面
            BeanUtils.copyProperties(flight,flightInfo);
            flightInfo.setChildren(seats);
            returnFlightInfoList.add(flightInfo);
        }

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("toList",flightInfoList);
        resultMap.put("returnList",returnFlightInfoList);

        return R.ok().data("map",resultMap);
    }





    //条件查询带分页的方法，加上 @RequestBody(required = false) ，请求方式必须改为,里面的参数可以为空，加上required = false
    @ApiOperation(value = "模糊查询分页航班列表")
    @PostMapping("pageFlightCondition/{current}/{limit}")
    public R pageFlightCondition(@PathVariable long current,@PathVariable long limit,
                                  @RequestBody(required = false) FlightQuery flightQuery) throws ParseException {
        //创建page对象
        Page<Flight> pageFlight = new Page<>(current,limit);

        //构建条件
        QueryWrapper<Flight> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String flightNumber = flightQuery.getFlightNumber();
        String airline = flightQuery.getAirline();
        String takeoffAirport = flightQuery.getTakeoffCity();
        String arrivalAirport = flightQuery.getArrivalCity();
        String takeoffTime = flightQuery.getTakeoffTime();
        String returnTime = flightQuery.getReturnTime();



        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(flightNumber)) {
            //构建条件,模糊查询
            wrapper.like("flight_number",flightNumber);
        }
        //构建条件,模糊查询
        if(!StringUtils.isEmpty(airline)) {
            wrapper.like("airline",airline);
        }
        //起飞机场
        if(!StringUtils.isEmpty(takeoffAirport)) {
            wrapper.like("takeoff_airport",takeoffAirport);
        }
        //到达机场
        if(!StringUtils.isEmpty(arrivalAirport)) {
            wrapper.like("arrival_airport",arrivalAirport);
        }
        // 日期大于等于
        if(!StringUtils.isEmpty(takeoffTime)) {
            wrapper.ge("takeoff_time",takeoffTime);

            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //定义日期格式化的格式
            Date classDate = format.parse(takeoffTime);//把字符串转化成指定格式的日期
            Calendar calendar = Calendar.getInstance(); //使用Calendar日历类对日期进行加减
            calendar.setTime(classDate);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            classDate = calendar.getTime();//获取加减以后的Date类型日期
            String day2 = format.format(classDate);//转化日历格式为上面格式字符串
            wrapper.le("takeoff_time",day2);
        }

        //根据日期进行排序
        wrapper.orderByDesc("takeoff_time");
        //调用方法实现条件查询分页
        flightService.page(pageFlight,wrapper);
        long total = pageFlight.getTotal();//总记录数
        List<Flight> records = pageFlight.getRecords(); //数据list集合
        return R.ok().data("total",total).data("rows",records);
    }



}

